package cn.xfdzx.android.apps.shop.activity;


import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Point;
import android.text.Editable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextWatcher;
import android.text.style.ForegroundColorSpan;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.CycleInterpolator;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.login.OneLoginMainActivity;
import cn.xfdzx.android.apps.shop.adapter.ShopSearchAdapter;
import cn.xfdzx.android.apps.shop.adapter.ShopSearchCarGoodsAdapter;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpActivity;
import cn.xfdzx.android.apps.shop.bean.BaseObjectBean;
import cn.xfdzx.android.apps.shop.bean.ConfirmGoodBeen;
import cn.xfdzx.android.apps.shop.bean.HomeSearchXFDGoodBeen;
import cn.xfdzx.android.apps.shop.bean.ShopCarListBeen;
import cn.xfdzx.android.apps.shop.bean.ShopGoodListBeen;
import cn.xfdzx.android.apps.shop.bean.ShopSearchBeen;
import cn.xfdzx.android.apps.shop.contract.GoodActionCallback;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.contract.ShopSearchContract;
import cn.xfdzx.android.apps.shop.presenter.ShopSearchPresenter;
import cn.xfdzx.android.apps.shop.util.EmptyUtil;
import cn.xfdzx.android.apps.shop.util.ProgressDialog;
import cn.xfdzx.android.apps.shop.util.Utils;
import cn.xfdzx.android.apps.shop.util.UtilsData;
import cn.xfdzx.android.apps.shop.view.MaxHeightRecyclerView;
import cn.xfdzx.android.apps.shop.view.ShoppingCartAnimationView;

import static cn.xfdzx.android.apps.shop.apps.Constants.APP_TOKEN;
import static cn.xfdzx.android.apps.shop.apps.Constants.SHOPID_69_NO;

//零售商品搜索
public class ShopSearchGoodsActivity extends BaseMvpActivity<ShopSearchPresenter> implements ShopSearchContract.View, GoodActionCallback {
    @BindView(R.id.tv_car_total_num)
    TextView tvCarTotalNum;
    @BindView(R.id.view_bg_black)
    View viewBgBlack;
    @BindView(R.id.ll_car)
    LinearLayout llCar;
    @BindView(R.id.max_height_rv)
    MaxHeightRecyclerView maxHeightRv;
    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.rv_list)
    RecyclerView rvShopGoods;
    @BindView(R.id.iv_shop_car)
    ImageView ivShopCar;
    @BindView(R.id.view_shop_car_bg)
    View viewShopCarBg;
    @BindView(R.id.tv_shop_car_price)
    TextView tvShopCarPrice;
    @BindView(R.id.rl_shop_car)
    RelativeLayout rlShopCar;
    @BindView(R.id.tv_total_goods_num)
    TextView tvTotalGoodsNum;
    @BindView(R.id.tv_to_settle_accounts)
    TextView tvToSettleAccounts;
   @BindView(R.id.tv_clear_all)
    TextView tvClearAll;
    @BindView(R.id.ll_hint)
    LinearLayout llHint;
    @BindView(R.id.tv_activity_man_jian)
    TextView tvActivityManJian;
    @BindView(R.id.et_home_search)
    EditText etHomeSearch;

    private int pages = 1;
    private int limit = Constants.LIMIT;//每页多少条
    private ShopSearchCarGoodsAdapter shopCarGoodsAdapter;
    //购物车里adpter
    private ArrayList<ShopCarListBeen.DataBean.ValidBean.GoodsBeanX> carList = new ArrayList();
    private ArrayList<ShopSearchBeen.DataBean> goodList = new ArrayList<>();//右边商品的列表
    private ShopSearchAdapter shopSearchAdapter;
    private String shopID;
    private String key = "";
    private  String user_curent_lng;
    private  String user_curent_lat;

    @Override
    public int getLayoutId() {
        return R.layout.activity_shop_search_goods;
    }

    @Override
    public void initView() {
          user_curent_lng = UtilsData.getPreference(ShopSearchGoodsActivity.this, "coordinates").getString("coordinates_lng");
          user_curent_lat = UtilsData.getPreference(ShopSearchGoodsActivity.this, "coordinates").getString("coordinates_lat");

        shopID = getIntent().getStringExtra("shop_id");
        mPresenter = new ShopSearchPresenter();
        mPresenter.attachView(this);
        rvShopGoods.setLayoutManager(new LinearLayoutManager(this));
        rvShopGoods.setItemAnimator(new DefaultItemAnimator());

        shopSearchAdapter = new ShopSearchAdapter(this, goodList, this);
        shopSearchAdapter.setOnItemClickListener(new ItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent = new Intent(ShopSearchGoodsActivity.this, GoodsDetailsActivity.class);
                intent.putExtra("good_id", goodList.get(position).getId() + "");

                startActivity(intent);
            }
        });
        rvShopGoods.setAdapter(shopSearchAdapter);

        maxHeightRv.setLayoutManager(new LinearLayoutManager(this));
        maxHeightRv.setItemAnimator(new DefaultItemAnimator());
        shopCarGoodsAdapter = new ShopSearchCarGoodsAdapter(this, carList, mPresenter);
        maxHeightRv.setAdapter(shopCarGoodsAdapter);

        etHomeSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().trim().length() == 0) {
                llHint.setVisibility(View.GONE);
                }
            }
        });

        etHomeSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId,
                                          KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    // 先隐藏键盘
                    ((InputMethodManager) etHomeSearch.getContext()
                            .getSystemService(Context.INPUT_METHOD_SERVICE))
                            .hideSoftInputFromWindow(ShopSearchGoodsActivity.this
                                            .getCurrentFocus().getWindowToken(),
                                    InputMethodManager.HIDE_NOT_ALWAYS);
                    // 搜索，进行自己要的操作...
                    key = etHomeSearch.getText().toString().trim();

                    mPresenter.initSearchData(true, true, user_curent_lng,user_curent_lat,"", pages + "", limit + "", "0", "0", shopID, key, "", "", "");

                    return true;
                }
                return false;
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        mPresenter.shopCarList(shopID, "0");
    }

    @Override
    public void showLoading() {
        if (!ProgressDialog.getInstance().materialDialog.isShowing()) {
            ProgressDialog.getInstance().show(this);
        }
    }

    @Override
    public void hideLoading() {
        ProgressDialog.getInstance().dismiss();
    }

    @Override
    protected void onDestroy() {

        super.onDestroy();
    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    public void onAddCarSuccess(ConfirmGoodBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            mPresenter.shopCarList(shopID, "0");
        } else {
            if (bean.getStatus() == Constants.LOGIN_CODE) {
                UtilsData.getPreference(this, "login").remove("head_url");
                UtilsData.getPreference(this, "login").remove("nickname");
                UtilsData.getPreference(this, "login").remove("is_store");
                UtilsData.getPreference(this, "login").remove("token");
                APP_TOKEN = "";
                Intent intent = new Intent(this, OneLoginMainActivity.class);
                startActivity(intent);
                return;
            }
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void onShopCarListSuccess(ShopCarListBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            carList.clear();
            if (bean.getData().getValid().size() != 0) {
                carList.addAll(bean.getData().getValid().get(0).getGoods());
            }
            for (ShopCarListBeen.DataBean.ValidBean.GoodsBeanX goodsBeanX : carList) {
                for (ShopSearchBeen.DataBean dataBean : goodList) {
                    if (goodsBeanX.getProductInfo().getId() == dataBean.getId()) {
                        dataBean.setNum(goodsBeanX.getCart_num());
                    }
                }
            }
            calculate();
            shopCarGoodsAdapter.notifyDataSetChanged();
            shopSearchAdapter.notifyDataSetChanged();
        } else {
//            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onUpdateCarNumSuccess(BaseObjectBean bean, int number, int one_position, int position) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            for (ShopSearchBeen.DataBean dataBean : goodList) {
                if (dataBean.getId() == carList.get(position).getProduct_id()) {
                    dataBean.setNum(number);
                }
            }
            carList.get(position).setCart_num(number);
            calculate();
            shopCarGoodsAdapter.notifyDataSetChanged();
            shopSearchAdapter.notifyDataSetChanged();
        } else {
            if (bean.getStatus() == Constants.LOGIN_CODE) {
                UtilsData.getPreference(this, "login").remove("head_url");
                UtilsData.getPreference(this, "login").remove("nickname");
                UtilsData.getPreference(this, "login").remove("is_store");
                UtilsData.getPreference(this, "login").remove("token");
                APP_TOKEN = "";
                Intent intent = new Intent(this, OneLoginMainActivity.class);
                startActivity(intent);
                return;
            }
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onRemoveGoodSuccess(BaseObjectBean bean, String good_id, int position,boolean isAll) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            if (isAll){
                mPresenter.shopCarList(shopID, "0");
            }else {
            for (ShopSearchBeen.DataBean dataBean : goodList) {
                if (dataBean.getId() == carList.get(position).getProduct_id()) {
                    dataBean.setNum(0);
                }
            }
            carList.remove(position);
            calculate();
            shopCarGoodsAdapter.notifyDataSetChanged();
            shopSearchAdapter.notifyDataSetChanged();}
        } else {
            if (bean.getStatus() == Constants.LOGIN_CODE) {
                UtilsData.getPreference(this, "login").remove("head_url");
                UtilsData.getPreference(this, "login").remove("nickname");
                UtilsData.getPreference(this, "login").remove("is_store");
                UtilsData.getPreference(this, "login").remove("token");
                APP_TOKEN = "";
                Intent intent = new Intent(this, OneLoginMainActivity.class);
                startActivity(intent);
                return;
            }
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onConfirmSuccess(ConfirmGoodBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            Intent confirmOrder = new Intent(this, ConfirmOrderActivity.class);
            confirmOrder.putExtra("confirm", bean.getData());
            startActivity(confirmOrder);
        } else if (bean.getStatus()==2000) {
            Toast.makeText(this, "此商铺已打烊", Toast.LENGTH_SHORT).show();
        }else {
            if (bean.getStatus() == Constants.LOGIN_CODE) {
                UtilsData.getPreference(this, "login").remove("head_url");
                UtilsData.getPreference(this, "login").remove("nickname");
                UtilsData.getPreference(this, "login").remove("is_store");
                UtilsData.getPreference(this, "login").remove("token");
                APP_TOKEN = "";
                Intent intent = new Intent(this, OneLoginMainActivity.class);
                startActivity(intent);
                return;
            }
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onInitSearchSuccess(ShopSearchBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            goodList.clear();
            goodList.addAll(bean.getData());
            for (ShopCarListBeen.DataBean.ValidBean.GoodsBeanX goodsBeanX : carList) {
                for (ShopSearchBeen.DataBean dataBean : goodList) {
                    if (goodsBeanX.getProductInfo().getId() == dataBean.getId()) {
                        dataBean.setNum(goodsBeanX.getCart_num());
                    }
                }
            }
            shopSearchAdapter.notifyDataSetChanged();
            if (goodList.size() == 0) {
                llHint.setVisibility(View.VISIBLE);
            } else {
                llHint.setVisibility(View.GONE);
            }
        } else {
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }


    @Override
    public void finishRefresh() {

    }

    @Override
    public void finishLoadMore() {

    }

    private String total_price = "0";
    private int total_num = 0;

    /**
     * 计算
     */
    public void calculate() {
        int num = 0;
        double price = 0;
        for (ShopCarListBeen.DataBean.ValidBean.GoodsBeanX goodsInfoBean : carList) {
            num += goodsInfoBean.getCart_num();
            price += goodsInfoBean.getCart_num() * Double.parseDouble(goodsInfoBean.getProductInfo().getPrice());
        }
        total_price = format1(price);
        total_num = num;
        tvTotalGoodsNum.setText(total_num + "");
        tvShopCarPrice.setText("¥" + total_price);
        tvCarTotalNum.setText("购物车(共" + carList.size() + "件商品)");
        if (total_num == 0) {
            viewShopCarBg.setBackgroundResource(R.drawable.round_hui_bg);
            tvToSettleAccounts.setBackgroundResource(R.color.color_999999);
            tvShopCarPrice.setText(getResources().getText(R.string.unselected_goods));
            tvShopCarPrice.setTextSize(14);
            tvShopCarPrice.setTextColor(getResources().getColor(R.color.color_999999));
            tvTotalGoodsNum.setVisibility(View.INVISIBLE);
        } else {
            if (tvTotalGoodsNum.getVisibility() != View.VISIBLE) {
                tvTotalGoodsNum.setVisibility(View.VISIBLE);
                viewShopCarBg.setBackgroundResource(R.drawable.round_orange_bg);
                tvToSettleAccounts.setBackgroundResource(R.color.color_FB9129);

                tvShopCarPrice.setTextSize(17);
                tvShopCarPrice.setTextColor(getResources().getColor(R.color.color_F93331));
            }
        }
        //活动是否显示
        if (UtilsData.getPreference(this,"activity").getBoolean("is_69")&&!SHOPID_69_NO.contains(shopID)){
            tvActivityManJian.setVisibility(View.VISIBLE);
            if(total_num >0){
                if (price<69){
                    String cha= format1( 69-price);
                    String hint="还差"+cha+"任减1件商品（至少选择5件商品）";
                    SpannableString spannableString = new SpannableString(hint);
                    //设置颜色
                    spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#F93331")), 2, 2+cha.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    tvActivityManJian.setText(spannableString);
                }else if (total_num<5){
                    int cha=5-total_num;
                    SpannableString spannableString = new SpannableString("还差"+cha+"件商品");
                    //设置颜色
                    spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#F93331")), 2, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    tvActivityManJian.setText(spannableString);
                }else {

                    tvActivityManJian.setText("已随机减免1件商品，可在结算页面查看");
                }
            }else {
                tvActivityManJian.setText("满69任减1件商品");
            }}
    }

    /**
     * 使用BigDecimal，保留小数点后两位
     */
    public String format1(double value) {

        BigDecimal bd = new BigDecimal(value);
        bd = bd.setScale(2, RoundingMode.HALF_UP);
        return bd.toString();
    }
    private Dialog dialog;
    @OnClick({R.id.tv_clear_all,R.id.iv_back, R.id.tv_to_settle_accounts, R.id.tv_search})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.tv_clear_all:
                dialog = Utils.addDialog(this, R.layout.dialog, R.string.are_you_sure_you_want_to_clear_this_car, R.string.confirm, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String ids = "";
                        for (ShopCarListBeen.DataBean.ValidBean.GoodsBeanX goodsBeanX : carList) {
                            if (ids.isEmpty()) {
                                ids = goodsBeanX.getId() + "";
                            } else {
                                ids = ids + "," + goodsBeanX.getId();
                            }
                        }
                        mPresenter.removeGood(ids + "", "" + "", 0,true);
                        dialog.dismiss();
                    }
                });
                dialog.show();
                break;
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_to_settle_accounts:
                if (EmptyUtil.isEmpty(APP_TOKEN)) {
                    Intent intent = new Intent(this, OneLoginMainActivity.class);
                    startActivity(intent);
                    return;
                }
                if (carList.size() != 0) {
                    String ids = "";
                    for (ShopCarListBeen.DataBean.ValidBean.GoodsBeanX goodsBeanX : carList) {
                        if (ids.isEmpty()) {
                            ids = goodsBeanX.getId() + "";
                        } else {
                            ids = ids + "," + goodsBeanX.getId();
                        }
                    }
                    mPresenter.confirm(ids, user_curent_lng, user_curent_lat);
                } else {
                    Toast.makeText(this, "请选择商品", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.tv_search:
                key = etHomeSearch.getText().toString().trim();

                mPresenter.initSearchData(true, true, user_curent_lng,user_curent_lat,"", pages + "", limit + "", "0", "0", shopID, key, "", "", "");

                break;
        }
    }


    @Override
    public void addAction(View view, int position, boolean is) {

        ShoppingCartAnimationView shoppingCartAnimationView = new ShoppingCartAnimationView(this);
        int postion[] = new int[2];
        view.getLocationInWindow(postion);
        shoppingCartAnimationView.setStartPosition(new Point(postion[0], postion[1]));
        ViewGroup rootView = (ViewGroup) getWindow().getDecorView();
        rootView.addView(shoppingCartAnimationView);
        int endPosition[] = new int[2];
        ivShopCar.getLocationInWindow(endPosition);
        shoppingCartAnimationView.setEndPosition(new Point(endPosition[0], endPosition[1]));
        shoppingCartAnimationView.startBeizerAnimation();
        rlShopCar.setAnimation(shakeAnimation(1));

        calculate();//本地总数

//修改商品
        if (goodList.get(position).getNum() == 0) {
            mPresenter.addCar(0 + "", "", goodList.get(position).getId() + "", "1");
        } else {
            for (int k = 0; k < carList.size(); k++) {
                if (goodList.get(position).getId() == carList.get(k).getProduct_id()) {
                    mPresenter.updateCarNum(carList.get(k).getId() + "", goodList.get(position).getNum() + 1, 0, k);//one_position无用
                    break;
                }
            }
        }

    }

    public Animation shakeAnimation(int CycleTimes) {
        Animation animation = new ScaleAnimation(1.0f, 1.2f, 1.0f, 1.2f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//        Animation translateAnimation = new TranslateAnimation(10, 10, 10, 10);
//        translateAnimation.setInterpolator(new CycleInterpolator(CycleTimes));
//        translateAnimation.setDuration(1000);
        animation.setInterpolator(new CycleInterpolator(CycleTimes));
        animation.setDuration(800);
        return animation;
    }

    @Override
    public void minusGood(int position, boolean is) {
        calculate();//本地总数
        for (int k = 0; k < carList.size(); k++) {
            if (goodList.get(position).getId() == carList.get(k).getProduct_id()) {
                if (goodList.get(position).getNum() == 1) {
                    //移除商品
                    mPresenter.removeGood(carList.get(k).getId() + "", carList.get(k).getId() + "", k,false);
                } else {
                    mPresenter.updateCarNum(carList.get(k).getId() + "", goodList.get(position).getNum() - 1, 0, k);//one_position无用
                }
                break;
            }
        }

    }


    @OnClick({R.id.tv_to_settle_accounts, R.id.iv_back,
            R.id.rl_shop_car, R.id.view_bg_black, R.id.ll_car})
    public void onclick(View view) {
        switch (view.getId()) {
            case R.id.tv_to_settle_accounts:
                if (EmptyUtil.isEmpty(APP_TOKEN)) {
                    Intent intent = new Intent(this, OneLoginMainActivity.class);
                    startActivity(intent);
                    return;
                }
                if (carList.size() != 0) {
                    String ids = "";
                    for (ShopCarListBeen.DataBean.ValidBean.GoodsBeanX goodsBeanX : carList) {
                        if (ids.isEmpty()) {
                            ids = goodsBeanX.getId() + "";
                        } else {
                            ids = ids + "," + goodsBeanX.getId();
                        }
                    }
                    mPresenter.confirm(ids, user_curent_lng, user_curent_lat);
                } else {
                    Toast.makeText(this, "请选择商品", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.iv_back:
                finish();
                break;
            case R.id.rl_shop_car:
                if (total_num == 0) {
                    return;
                }
                if (llCar.getVisibility() == View.GONE) {
                    startTranslationAnimation();
                } else {
//                    关闭平移动画
                    endTranslationAnimation();
                }
                break;
            case R.id.view_bg_black:
                // 关闭平移动画
                endTranslationAnimation();
                break;
            case R.id.ll_car:

                break;

        }
    }

    /*
     * 方法名：startTranslationAnimation()
     * 功    能：开启平移动画
     * 参    数：无
     * 返回值：无
     */
    private void startTranslationAnimation() {
//        TranslateAnimation startTranslateAnim = new TranslateAnimation(0, 0, -height, 0);
//        startTranslateAnim.setDuration(300);
//        llCar.startAnimation(startTranslateAnim);


        //设置动画，从自身位置的最下端向上滑动了自身的高度，持续时间为500ms
        final TranslateAnimation ctrlAnimation = new TranslateAnimation(
                TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0,
                TranslateAnimation.RELATIVE_TO_SELF, 1, TranslateAnimation.RELATIVE_TO_SELF, 0);
        ctrlAnimation.setDuration(300);     //设置动画的过渡时间
        llCar.postDelayed(new Runnable() {
            @Override
            public void run() {
                tvCarTotalNum.setText("购物车(共" + carList.size() + "件商品)");
                viewBgBlack.setVisibility(View.VISIBLE);
                llCar.setVisibility(View.VISIBLE);
                llCar.startAnimation(ctrlAnimation);
            }
        }, 300);

    }

    /*
     * 方法名：endTranslationAnimation()
     * 功    能：关闭平移动画
     * 参    数：无
     * 返回值：无
     */
    private void endTranslationAnimation() {
//        TranslateAnimation endTranslateAnim= new TranslateAnimation(0, 0, 0, -height);
//        endTranslateAnim.setDuration(300);
//        llCar.startAnimation(endTranslateAnim);

//        设置动画，从自身位置的最下端向上滑动了自身的高度，持续时间为500ms
        final TranslateAnimation ctrlAnimation = new TranslateAnimation(
                TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0,
                TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 1);
        ctrlAnimation.setDuration(300);     //设置动画的过渡时间
        llCar.postDelayed(new Runnable() {
            @Override
            public void run() {
                viewBgBlack.setVisibility(View.GONE);
                llCar.setVisibility(View.GONE);
                llCar.startAnimation(ctrlAnimation);
            }
        }, 300);

    }

}
